using System;
using System.Collections.Generic;
using System.Text;
using System.Data;using THSERP.BusinessEntities;using THSERP.BaseProvider;
using THSLib;using THSERP.BaseProvider;

namespace THSERP.Modules.Project
{
    public class ProjectEntities : ERPModuleEntities
    {
        #region Declare Constant
        public readonly static String strPMProjectsObjectName = "PMProjects";
        public readonly static String strPMProjectPhasesObjectName = "PMProjectPhases";
        public readonly static String strPMProjectPhaseResourcesObjectName = "PMProjectPhaseResources";
        #endregion
        
        #region Declare all entities variables
        private PMProjectsInfo _pMProjectsObject;
        private PMProjectPhasesInfo _pMProjectPhasesObject;
        private PMProjectPhaseResourcesInfo _pMProjectPhaseResourcesObject;

        private THSItemsEntityList<ProjectPhaseItemsEntity> _lstPMProjectPhaseItemsEntity;
        private THSList<PMProjectPhaseResourcesInfo> _lstPMProjectPhaseResourceEmployees;
        private THSList<PMProjectPhaseResourcesInfo> _lstPMProjectPhaseResourceEquipments;
        private THSList<PMProjectPhaseResourcesInfo> _lstPMProjectPhaseResourceMaterials;
        #endregion

        #region Public Properties
        public PMProjectsInfo PMProjectsObject
        {
            get
            {
                return _pMProjectsObject;
            }
            set
            {
                _pMProjectsObject = value;
            }
        }

        public PMProjectPhasesInfo PMProjectPhasesObject
        {
            get
            {
                return _pMProjectPhasesObject;
            }
            set
            {
                _pMProjectPhasesObject = value;
            }
        }

        public PMProjectPhaseResourcesInfo PMProjectPhaseResourcesObject
        {
            get
            {
                return _pMProjectPhaseResourcesObject;
            }
            set
            {
                _pMProjectPhaseResourcesObject = value;
            }
        }

        public THSItemsEntityList<ProjectPhaseItemsEntity> PMProjectPhaseItemsEntityList
        {
            get
            {
                return _lstPMProjectPhaseItemsEntity;
            }
            set
            {
                _lstPMProjectPhaseItemsEntity = value;
            }
        }

        public THSList<PMProjectPhaseResourcesInfo> PMProjectPhaseResourceEmployeesList
        {
            get
            {
                return _lstPMProjectPhaseResourceEmployees;
            }
            set
            {
                _lstPMProjectPhaseResourceEmployees = value;
            }
        }

        public THSList<PMProjectPhaseResourcesInfo> PMProjectPhaseResourceEquipmentsList
        {
            get
            {
                return _lstPMProjectPhaseResourceEquipments;
            }
            set
            {
                _lstPMProjectPhaseResourceEquipments = value;
            }
        }

        public THSList<PMProjectPhaseResourcesInfo> PMProjectPhaseResourceMaterialsList
        {
            get
            {
                return _lstPMProjectPhaseResourceMaterials;
            }
            set
            {
                _lstPMProjectPhaseResourceMaterials = value;
            }
        }
        #endregion

        #region Constructor
        public ProjectEntities()
            : base()
        {
            PMProjectsObject = new PMProjectsInfo();
            PMProjectPhasesObject = new PMProjectPhasesInfo();
            PMProjectPhaseResourcesObject = new PMProjectPhaseResourcesInfo();

            PMProjectPhaseItemsEntityList = new THSItemsEntityList<ProjectPhaseItemsEntity>();
            PMProjectPhaseResourceEmployeesList = new THSList<PMProjectPhaseResourcesInfo>();
            PMProjectPhaseResourceEquipmentsList = new THSList<PMProjectPhaseResourcesInfo>();
            PMProjectPhaseResourceMaterialsList = new THSList<PMProjectPhaseResourcesInfo>();
        }

        #endregion

        #region Init Main Object,Module Objects functions
        public override void InitMainObject()
        {

            MainObject = PMProjectsObject;
        }

        public override void InitModuleObjects()
        {
            ModuleObjects.Add(ProjectEntities.strPMProjectPhasesObjectName, PMProjectPhasesObject);
            ModuleObjects.Add(ProjectEntities.strPMProjectPhaseResourcesObjectName, PMProjectPhaseResourcesObject);
        }

        public override void InitModuleObjectList()
        {
            PMProjectPhaseItemsEntityList.InitTHSList(this, ProjectEntities.strPMProjectsObjectName, ProjectEntities.strPMProjectPhasesObjectName);
            PMProjectPhaseResourceEmployeesList.InitTHSList(this, ProjectEntities.strPMProjectPhasesObjectName, ProjectEntities.strPMProjectPhaseResourcesObjectName);
            PMProjectPhaseResourceEquipmentsList.InitTHSList(this, ProjectEntities.strPMProjectPhasesObjectName, ProjectEntities.strPMProjectPhaseResourcesObjectName);
            PMProjectPhaseResourceMaterialsList.InitTHSList(this, ProjectEntities.strPMProjectPhasesObjectName, ProjectEntities.strPMProjectPhaseResourcesObjectName);
        }

        public override void InitGridControlInTHSList()
        {
            PMProjectPhaseItemsEntityList.InitTHSListGridControl();
            PMProjectPhaseResourceEmployeesList.InitTHSListGridControl("fld_dgcPMProjectPhaseResourceEmployees");
            PMProjectPhaseResourceEquipmentsList.InitTHSListGridControl("fld_dgcPMProjectPhaseResourceEquipments");
            PMProjectPhaseResourceMaterialsList.InitTHSListGridControl("fld_dgcPMProjectPhaseResourceMaterials");
        }

        public override void SetDefaultModuleObjectsList()
        {
            try
            {
                PMProjectPhaseItemsEntityList.SetDefaultListAndRefreshGridControl();
                PMProjectPhaseResourceEmployeesList.SetDefaultListAndRefreshGridControl();
                PMProjectPhaseResourceEquipmentsList.SetDefaultListAndRefreshGridControl();
                PMProjectPhaseResourceMaterialsList.SetDefaultListAndRefreshGridControl();
            }
            catch (Exception)
            {
                return;
            }
        }

        #endregion

        #region Invalidate Module Objects functions
        public override void InvalidateModuleObject(ERPModuleItemsEntity entItem, string strModuleObjectName)
        {
            base.InvalidateModuleObject(entItem, strModuleObjectName);
            if (strModuleObjectName == ProjectEntities.strPMProjectPhasesObjectName)
            {
                ProjectPhaseItemsEntity entProjectPhaseItem = (ProjectPhaseItemsEntity)entItem;
                PMProjectPhaseResourcesController objProjectPhaseResourcesController = new PMProjectPhaseResourcesController();
                if (entProjectPhaseItem.PMProjectPhaseResourcesList == null)
                {
                    entProjectPhaseItem.PMProjectPhaseResourcesList = new THSList<PMProjectPhaseResourcesInfo>();
                    DataSet ds = objProjectPhaseResourcesController.GetAllDataByForeignColumn("FK_PMProjectPhaseID", entProjectPhaseItem.PMProjectPhaseID);
                    if (ds.Tables.Count > 0)
                    {
                        foreach (DataRow row in ds.Tables[0].Rows)
                        {
                            PMProjectPhaseResourcesInfo objProjectPhaseResourcesInfo = (PMProjectPhaseResourcesInfo)objProjectPhaseResourcesController.GetObjectFromDataRow(row);
                            entProjectPhaseItem.PMProjectPhaseResourcesList.Add(objProjectPhaseResourcesInfo);
                        }
                    }
                }

                List<PMProjectPhaseResourcesInfo> lstPMProjectPhaseResourceEmployees = new List<PMProjectPhaseResourcesInfo>();
                List<PMProjectPhaseResourcesInfo> lstPMProjectPhaseResourceEquipments = new List<PMProjectPhaseResourcesInfo>();
                List<PMProjectPhaseResourcesInfo> lstPMProjectPhaseResourceMaterials = new List<PMProjectPhaseResourcesInfo>();
                foreach (PMProjectPhaseResourcesInfo objProjectPhaseResourcesInfo in entProjectPhaseItem.PMProjectPhaseResourcesList)
                {
                    switch (objProjectPhaseResourcesInfo.PMProjectPhaseResourceType)
                    {
                        case ProjectModule.cstProjectPhaseResourceEmployee:
                            lstPMProjectPhaseResourceEmployees.Add(objProjectPhaseResourcesInfo);
                            break;
                        case ProjectModule.cstProjectPhaseResourceEquipment:
                            lstPMProjectPhaseResourceEquipments.Add(objProjectPhaseResourcesInfo);
                            break;
                        case ProjectModule.cstProjectPhaseResourceMaterial:
                            lstPMProjectPhaseResourceMaterials.Add(objProjectPhaseResourcesInfo);
                            break;
                    }
                }

                this.PMProjectPhaseResourceEmployeesList.Invalidate(lstPMProjectPhaseResourceEmployees);
                this.PMProjectPhaseResourceEquipmentsList.Invalidate(lstPMProjectPhaseResourceEquipments);
                this.PMProjectPhaseResourceMaterialsList.Invalidate(lstPMProjectPhaseResourceMaterials);
            }
        }

        public override void InvalidateModuleObjects(int iObjectID)
        {
            PMProjectPhaseItemsEntityList.Invalidate(iObjectID);
        }
        #endregion

        #region Save Module Objects functions

        public override void SaveModuleObjects()
        {
            if (PMProjectPhaseItemsEntityList.GridView.FocusedRowHandle >= 0)
            {
                ProjectPhaseItemsEntity entItem = PMProjectPhaseItemsEntityList[PMProjectPhaseItemsEntityList.CurrentIndex];
                entItem.PMProjectPhaseResourcesList.Clear();
                foreach (PMProjectPhaseResourcesInfo objProjectPhaseResourcesInfo in this.PMProjectPhaseResourceEmployeesList)
                    entItem.PMProjectPhaseResourcesList.Add((PMProjectPhaseResourcesInfo)objProjectPhaseResourcesInfo.Clone());
                foreach (PMProjectPhaseResourcesInfo objProjectPhaseResourcesInfo in this.PMProjectPhaseResourceEquipmentsList)
                    entItem.PMProjectPhaseResourcesList.Add((PMProjectPhaseResourcesInfo)objProjectPhaseResourcesInfo.Clone());
                foreach (PMProjectPhaseResourcesInfo objProjectPhaseResourcesInfo in this.PMProjectPhaseResourceMaterialsList)
                    entItem.PMProjectPhaseResourcesList.Add((PMProjectPhaseResourcesInfo)objProjectPhaseResourcesInfo.Clone());
            }

            int iProjectID = ((PMProjectsInfo)((BaseModuleERP)Module).CurrentModuleEntity.MainObject).PMProjectID;
            if (iProjectID > 0)
            {
                ProjectEntities entity = (ProjectEntities)((BaseModuleERP)Module).CurrentModuleEntity;
                PMProjectPhasesController objProjectPhasesController = new PMProjectPhasesController();
                PMProjectPhaseResourcesController objProjectPhaseResourcesController = new PMProjectPhaseResourcesController();

                objProjectPhasesController.DeleteByForeignColumn("FK_PMProjectID", iProjectID);
                foreach (ProjectPhaseItemsEntity entItem in entity.PMProjectPhaseItemsEntityList)
                {
                    objProjectPhaseResourcesController.DeleteByForeignColumn("FK_PMProjectPhaseID", entItem.PMProjectPhaseID);

                    int iProjectPhaseID;
                    PMProjectPhasesInfo objProjectPhasesInfo = (PMProjectPhasesInfo)entItem.SetToBusinessObject(ProjectEntities.strPMProjectPhasesObjectName);
                    objProjectPhasesInfo.FK_PMProjectID = iProjectID;
                    if (objProjectPhasesController.IsExist(objProjectPhasesInfo.PMProjectPhaseID))
                        iProjectPhaseID = objProjectPhasesController.UpdateObject(objProjectPhasesInfo);
                    else
                        iProjectPhaseID = objProjectPhasesController.CreateObject(objProjectPhasesInfo);

                    if (iProjectPhaseID > 0)
                    {
                        if (entItem.PMProjectPhaseResourcesList != null)
                        {
                            foreach (PMProjectPhaseResourcesInfo objProjectPhaseResourcesInfo in entItem.PMProjectPhaseResourcesList)
                            {
                                objProjectPhaseResourcesInfo.FK_PMProjectPhaseID = iProjectPhaseID;
                                if (objProjectPhaseResourcesController.IsExist(objProjectPhaseResourcesInfo.PMProjectPhaseResourceID))
                                    objProjectPhaseResourcesController.UpdateObject(objProjectPhaseResourcesInfo);
                                else
                                    objProjectPhaseResourcesController.CreateObject(objProjectPhaseResourcesInfo);
                            }
                        }
                    }
                }
            }
        }
        #endregion

        public override void SetValuesAfterValidateProduct(int iICProductID)
        {
            ICProductsController objProductsController = new ICProductsController();
            ICProductsInfo objProductsInfo = (ICProductsInfo)objProductsController.GetObjectByID(iICProductID);
            if (objProductsInfo != null)
            {
                PMProjectPhaseResourcesInfo objProjectPhaseResourcesInfo = (PMProjectPhaseResourcesInfo)ModuleObjects[ProjectEntities.strPMProjectPhaseResourcesObjectName];
                objProjectPhaseResourcesInfo.PMProjectPhaseResourceDesc = objProductsInfo.ICProductDesc;   
                objProjectPhaseResourcesInfo.PMProjectPhaseResourceBasicUnit = objProductsInfo.ICProductBasicUnit;
                objProjectPhaseResourcesInfo.PMProjectPhaseResourcePackagingUnit = objProductsInfo.ICProductPackUnit;
                objProjectPhaseResourcesInfo.PMProjectPhaseResourceUnitVolumn = objProductsInfo.ICProductWidth;
                objProjectPhaseResourcesInfo.PMProjectPhaseResourceUnitWeight = objProductsInfo.ICProductWeight;
                objProjectPhaseResourcesInfo.PMProjectPhaseResourceUnitCost = objProductsInfo.ICProductSupplierPrice;
                objProjectPhaseResourcesInfo.PMProjectPhaseResourceQty = 1;
                UpdateModuleObjectBindingSource(ProjectEntities.strPMProjectPhaseResourcesObjectName);
            }
        }

        public override void SetValuesAfterValidateProductQty()
        {
            PMProjectPhaseResourcesInfo objProjectPhaseResourcesInfo = (PMProjectPhaseResourcesInfo)ModuleObjects[ProjectEntities.strPMProjectPhaseResourcesObjectName];
            objProjectPhaseResourcesInfo.PMProjectPhaseResourceTotalCost = objProjectPhaseResourcesInfo.PMProjectPhaseResourceUnitCost * objProjectPhaseResourcesInfo.PMProjectPhaseResourceQty;
            UpdateModuleObjectBindingSource(ProjectEntities.strPMProjectPhaseResourcesObjectName);
        }

        public override void SetValuesAfterValidateProductUnitPrice()
        {
            PMProjectPhaseResourcesInfo objProjectPhaseResourcesInfo = (PMProjectPhaseResourcesInfo)ModuleObjects[ProjectEntities.strPMProjectPhaseResourcesObjectName];
            objProjectPhaseResourcesInfo.PMProjectPhaseResourceTotalCost = objProjectPhaseResourcesInfo.PMProjectPhaseResourceUnitCost * objProjectPhaseResourcesInfo.PMProjectPhaseResourceQty;
            UpdateModuleObjectBindingSource(ProjectEntities.strPMProjectPhaseResourcesObjectName);
        }
    }
}
